﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using NS.Utilities;
using NS.BLL.Systems;
using BLL;
using Model;

public partial class Platform_ParkManage : System.Web.UI.Page
{
    private string ParentID = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ParentID"] != null)
        {
            ParentID = Request.QueryString["ParentID"];
        }
        if (!Page.IsPostBack)
        {
            GridViewDataBind();
            LabPath.Text = CreatePath(ParentID);
        }
    }
    private string CreatePath(string ParentID)
    {
        StringBuilder MenuPath = new StringBuilder("<a href=ParkManage.aspx?CommuntyID=" + Request.QueryString["CommuntyID"] + "&ParentID=0>全部</a>>>");
        Park Model = ParkService.GetParkByParkID(Convert.ToInt64(ParentID));
        if (Model != null)
        {
            string[] MyArray = (Model.ParentPath + "," + ParentID).Split(',');
            foreach (string TypeID in MyArray)
            {
                if (TypeID != "0")
                {
                    string strTypeName = UtilityService.GetFieldValueByPK("Park", "ParkName", "ParkID", TypeID)[0].ToString();
                    MenuPath.Append("<a href=ParkManage.aspx?CommuntyID=" + Request.QueryString["CommuntyID"] + "&ParentID=" + TypeID + ">" + strTypeName + "</a>>>");
                }
            }
        }
        return MenuPath.ToString();
    }
    private void GridViewDataBind()
    {
        FGridView1.DataSource = ParkService.GetAllPark(Request.QueryString["CommuntyID"], ParentID);
        FGridView1.DataBind();
    }
    protected void btnBindData_Click(object sender, EventArgs e)
    {
        GridViewDataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Park Model = new Park();
        Model.ParkName = txtParkName.Text.Trim();
        Model.CommuntyID = Convert.ToInt64(Request.QueryString["CommuntyID"]);
        Model.ParentID = Convert.ToInt64(ParentID);
        if (ParentID != "0")
        {
            object[] Info = UtilityService.GetFieldValueByPK("Park", "ParentPath,Depth", "ParkID", ParentID);
            Model.ParentPath = Info[0].ToString() + "," + ParentID;
            Model.Depth = Convert.ToInt32(Info[1]) + 1;
        }
        else
        {
            Model.ParentPath = "0";
            Model.Depth = 0;
        }
        if (ParkService.AddPark(Model) > 0)
        {
            GridViewDataBind();
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string ID = ((Button)sender).CommandArgument.ToString();
        ParkService.DeleteParkByParkID(Convert.ToInt32(ID));
        GridViewDataBind();
    }
}